package usually;
//643. 子数组最大平均数 I
public class Num643_findMaxAverage {
    /**
     * 滑动窗口固定
     * @param nums
     * @param k
     * @return
     */
    public double findMaxAverage(int[] nums, int k) {
        int sumMax = 0;
        int sum = 0;
        int n = nums.length;
        //求前k个连续子数组的和
        for (int i = 0; i < k; i++) {
            sum += nums[i];
        }
        sumMax = sum;
        for (int i = k; i < n; i++) {
            sum = sum - nums[i - k] + nums[i] ;
            sumMax = Math.max(sum , sumMax);
        }
        return 1.0 * sumMax / k ;

    }
}
